package com.gc.ip.demo;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.gc.ip.IPAddress;
import com.gc.ip.IPType;
import com.gc.ip.JIPAddress;
import com.gc.ip.JIPAddressCellEditor;
import com.gc.ip.JIPAddressCellRenderer;
import com.gc.ip.JIPv4Editor;
import com.gc.ip.JLabelIPIndicator;

public class JIPAddressTableDemo extends JPanel
{
	private static final long serialVersionUID = -6570769092987106181L;

	private JTable renderPrefixTable = null;
	private JTable renderNonPrefixTable = null;
	private JTable nonRenderPrefixTable = null;
	private JTable nonRenderNonPrefixTable = null;
	private List<JTable> anyTables = null;
	
	private JTable v4IndicatorTable = null;
	private JTable v4NonIndicatorTable = null;
	private List<JTable> ipv4Tables = null;
	
	public boolean isShowV4 = false;

	public JIPAddressTableDemo()
	{
		anyTables = new ArrayList<JTable>();
		ipv4Tables = new ArrayList<JTable>();
		
		isShowV4 = false;
		
		initGUI();
		
		for(JTable table : anyTables)
		{
			initAnyTableData(table);
		}
		
		if(isShowV4)
		{
			for(JTable table : ipv4Tables)
			{
				initIPv4TableData(table);
			}		
		}
	}

	private void initGUI()
	{
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 5, 0, 5), 0, 0);
		
		JIPAnyTableBuilder tb = new JIPAnyTableBuilder();
		
		tb.addColumnName("IPv4 & IPv6").addColumnName("IPv4 Only").addColumnName("IPv6 Only");
		tb.addColumnWidth(300).addColumnWidth(240).addColumnWidth(300);
		tb.addColumnIPType(IPType.IPv4_v6).addColumnIPType(IPType.IPv4).addColumnIPType(IPType.IPv6);
		
		//table 1
		tb.hasRenderer(true).hasPrefixLength(true);
		renderPrefixTable = tb.build();
		gbc.gridy = 0;
		this.add(buildTablePane(renderPrefixTable, "Renderer & PrefixLength"), gbc);
		anyTables.add(renderPrefixTable);

		//table 2
		tb.hasRenderer(true).hasPrefixLength(false);
		renderNonPrefixTable = tb.build();
		gbc.gridy++;
		this.add(buildTablePane(renderNonPrefixTable, "Renderer & Non-PrefixLength"), gbc);
		anyTables.add(renderNonPrefixTable);
		
		//table 3
		tb.hasRenderer(false).hasPrefixLength(true);
		nonRenderPrefixTable = tb.build();
		gbc.gridy++;
		this.add(buildTablePane(nonRenderPrefixTable, "Non-Renderer & PrefixLength"), gbc);
		anyTables.add(nonRenderPrefixTable);
				
		//table 4
		tb.hasRenderer(false).hasPrefixLength(false);
		nonRenderNonPrefixTable = tb.build();
		gbc.gridy++;
		this.add(buildTablePane(nonRenderNonPrefixTable, "Non-Renderer & Non-PrefixLength"), gbc);
		anyTables.add(nonRenderNonPrefixTable);
		
		if(isShowV4)
		{
			//table 5
			v4IndicatorTable = buildIPv4Table(true);
			gbc.gridy++;
			this.add(buildTablePane(v4IndicatorTable, "IPv4 Table with Indicator"), gbc);
			ipv4Tables.add(v4IndicatorTable);
			
			//table 5
			v4NonIndicatorTable = buildIPv4Table(false);
			gbc.gridy++;
			this.add(buildTablePane(v4NonIndicatorTable, "IPv4 Table without Indicator"), gbc);
			ipv4Tables.add(v4NonIndicatorTable);			
		}
	}
	
	private JScrollPane buildTablePane(JTable table, String borderTitle)
	{
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(table);
		scrollPane.setBorder(new TitledBorder(borderTitle));
		return scrollPane;
	}
	
	private void initAnyTableData(JTable table)
	{
		Vector dataVec = new Vector();
		
		//row1
		Vector rowData = new Vector();
		rowData.add(new IPAddress("2001:0db8:85a3:08d3:1319:8a2e:0370::", "FC00::"));
		rowData.add(new IPAddress("192.168.0.12", 5));
		rowData.add(new IPAddress("2001:0db8:85a3:08d3:1319:8a2e:0370::", 10));
		dataVec.add(rowData);
		
		//row2
		rowData = new Vector();
		rowData.add(new IPAddress("192.168.0.12", "255.255.255.128"));
		rowData.add(new IPAddress("192.168.0.12"));
		rowData.add(new IPAddress("2001:0db8:85a3:08d3:1319:8a2e:0370::", 8));
		dataVec.add(rowData);
		
		DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
		tableModel.getDataVector().addAll(dataVec);
		tableModel.fireTableDataChanged();
	}
	
	private void initIPv4TableData(JTable table)
	{
		Vector dataVec = new Vector();
		
		//row1
		Vector rowData = new Vector();
		rowData.add(new IPAddress("192.168.0.1", 24));
		rowData.add(new IPAddress("192.168.0.12"));
		rowData.add(new IPAddress("10.0.0.8", 32));
		rowData.add(new IPAddress("127.0.0.1"));
		dataVec.add(rowData);
		
		//row2
		rowData = new Vector();
		rowData.add(new IPAddress("88.58.25.65", 0));
		rowData.add(new IPAddress("8.8.8.8"));
		rowData.add(new IPAddress("5.5.5.5", 32));
		rowData.add(new IPAddress("7.7.7.7"));
		dataVec.add(rowData);
		
		DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
		tableModel.getDataVector().addAll(dataVec);
		tableModel.fireTableDataChanged();
	}
	
	private JTable buildIPv4Table(boolean hasIndicator)
	{
		Vector columnNames = new Vector();
		columnNames.add("Render & Prefix");
		columnNames.add("Render & Non-Prefix");
		columnNames.add("Non-Render & Prefix");
		columnNames.add("Non-Render & Non-Prefix");
		
		Vector<Integer> columnWidths = new Vector<Integer>();
		columnWidths.add(250);
		columnWidths.add(190);
		columnWidths.add(250);
		columnWidths.add(150);
		
		Vector<Boolean> hasPrefixVec = new Vector<Boolean>();
		hasPrefixVec.add(Boolean.TRUE);
		hasPrefixVec.add(Boolean.FALSE);
		hasPrefixVec.add(Boolean.TRUE);
		hasPrefixVec.add(Boolean.FALSE);
		
		Vector<Boolean> hasRenderVec = new Vector<Boolean>();
		hasRenderVec.add(Boolean.TRUE);
		hasRenderVec.add(Boolean.TRUE);
		hasRenderVec.add(Boolean.FALSE);
		hasRenderVec.add(Boolean.FALSE);

		// Table Model
		DefaultTableModel tableModel = new DefaultTableModel((Vector) null, (Vector) columnNames);

		// Init table
		JTable table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		// Columns
		JIPAddressCellEditor cellEditor = null;
		JIPAddressCellRenderer renderer = null;
		TableColumn column = null;
		JIPAddress jIpAddress = null;
		for(int i = 0, columnCount = table.getColumnCount(); i < columnCount; i++)
		{
			column = table.getColumnModel().getColumn(i);

			if(i < columnCount)
			{
				column.setPreferredWidth(columnWidths.get(i));
			}
			
			JIPv4Editor ipv4Editor = new JIPv4Editor(hasPrefixVec.get(i));

			if(hasIndicator)
			{
				jIpAddress = new JIPAddress(ipv4Editor, new JLabelIPIndicator(IPType.IPv4));
			}
			else
			{
				jIpAddress = new JIPAddress(ipv4Editor, null);
			}
			
			cellEditor = new JIPAddressCellEditor(jIpAddress);
			column.setCellEditor(cellEditor);

			if(hasRenderVec.get(i))
			{
				ipv4Editor = new JIPv4Editor(hasPrefixVec.get(i));

				if(hasIndicator)
				{
					jIpAddress = new JIPAddress(ipv4Editor, new JLabelIPIndicator(IPType.IPv4));
				}
				else
				{
					jIpAddress = new JIPAddress(ipv4Editor, null);
				}
				
				renderer = new JIPAddressCellRenderer(jIpAddress);
				column.setCellRenderer(renderer);
			}
		}

		table.setRowHeight(28);
		return table;
	}
	
	public static void centerInScreen(Window f)
	{
		Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
		Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
			f.getGraphicsConfiguration());

		// Make sure we don't place the demo off the screen.
		int centerWidth = screenRect.width < f.getSize().width ? screenRect.x : screenRect.x
			+ screenRect.width / 2 - f.getSize().width / 2;
		int centerHeight = screenRect.height < f.getSize().height ? screenRect.y : screenRect.y
			+ screenRect.height / 2 - f.getSize().height / 2;

		centerHeight = centerHeight < screenInsets.top ? screenInsets.top : centerHeight;

		f.setLocation(centerWidth, centerHeight);
	}
	
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("JIPAddressTableDemo");
		
		JIPAddressTableDemo tableDemo = new JIPAddressTableDemo();
		frame.getContentPane().add(tableDemo);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = null;
		if(tableDemo.isShowV4)
		{
			dim = new Dimension(900, 800);
		}
		else
		{
			dim = new Dimension(900, 600);
		}
        frame.setPreferredSize(dim);
        frame.pack();
        centerInScreen(frame);
        frame.setVisible(true);
        
        /*
        //Create JTable
        JTable table = new JTable();
        
        //Initialize table
        ...
        
        //Find the ip address column
        TableColumn ipAddressColumn = table.getColumn("IP Address");
        
        //Create a JIPAddress component
        //has prefix length and only allows IPv4
        JIPAddress jIPAddress = new JIPAddress(true, IPType.IPv4);
        
        //Create Table CellRenderer and then set
        JIPAddressCellRenderer renderer = new JIPAddressCellRenderer(jIPAddress);
        ipAddressColumn.setCellRenderer(renderer);
        
        //Create Table CellEditor and then set
        JIPAddressCellEditor editor = new JIPAddressCellEditor(jIPAddress);
        ipAddressColumn.setCellEditor(editor);
        */
	}
}
